#include "ConfigDialog.h"
#include "ui_ConfigDialog.h"

#include <QtGui>
#include "AbstractSettingsPage.h"

//! @class ConfigDialog
ConfigDialog::ConfigDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::ConfigDialog)
{
    ui->setupUi(this);
    extendUi();
}

ConfigDialog::~ConfigDialog()
{
    delete ui;
}

void ConfigDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void ConfigDialog::extendUi()
{
    connect(ui->contents, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
            this, SLOT(changePage(QListWidgetItem*, QListWidgetItem*)));
    connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(save()));

    ui->contents->setCurrentRow(0);
}

void ConfigDialog::changePage(QListWidgetItem* currentItem, QListWidgetItem* previousItem)
{
    if (!currentItem)
        currentItem = previousItem;
    ui->pages->setCurrentIndex(ui->contents->row(currentItem));
}

//! private
void ConfigDialog::save()
{
    for (int i = 0; i < ui->pages->count(); ++i) {
        AbstractSettingsPage* page = qobject_cast<AbstractSettingsPage*>(ui->pages->widget(i));
        page->save();
    }
}
